package neuron.file;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import neuron.Layers;
import neuron.Network;

/**
 * Auxiliary data used to show neurons.
 * Tries to read $HOME/neuroview_auxdata.xls for soma-pia distances et al.
 *    
 * @author risto
 *
 */
public class AuxData {

	private static ExcelDB db;
	private static Map<String, Map<String,String>> data;
	
	static {
		data = new HashMap<String, Map<String,String>>();
		try {
			db = new ExcelDB(System.getProperty("user.home") + File.separator + "neuroview_auxdata.xls");
			for (Map<String,String> m : db.getValues()) {
				String file = m.get("FILE").toUpperCase();
				if (data.containsKey(file)) {
					System.err.println("DUPLICATE KEY " + file);
				}
//				System.out.println("ADD FILE '" + file + "'");
				data.put(file, m);
			}
		} catch (Exception e) {
			System.err.println("Can't open aux data db!");
		//	e.printStackTrace();
		}
	}

	public static void openDB(File dir)
	{
		try {
			File dbFile = new File(dir, "neuroview_auxdata.xls");
			db = new ExcelDB(dbFile.getAbsolutePath());
			for (Map<String,String> m : db.getValues()) {
				String file = m.get("FILE").toUpperCase();
				if (data.containsKey(file)) {
//					System.err.println("DUPLICATE KEY " + file);
				}
//				System.out.println("ADD FILE '" + file + "'");
				data.put(file, m);
			}
		} catch (Exception e) {
			System.out.println("Can't open aux data db in " + dir.getAbsolutePath() + ".");
		//	e.printStackTrace();
		}

	}

	public static enum Type { PIA_WM, PIA_LOWERL4, PIA_SOMA, AGE }; 
	
	public static int getValue(String filename, Type type)
	{
		if (filename == null || data == null) return -1;
		Map<String,String> m = data.get(filename.toUpperCase());
		if (m == null) return -1;
		String s;

		switch(type) {
			case PIA_WM:
				s = m.get("PIA-WM");
				return Integer.parseInt(s);
			case PIA_LOWERL4:
				s = m.get("PIA-LOWERL4");
				return Integer.parseInt(s);
			case PIA_SOMA:
				s = m.get("PIA-SOMA");
				return Integer.parseInt(s);
			case AGE:
				s = m.get("AGE");
				return Integer.parseInt(s);
		}
		
		return -1;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(AuxData.getValue("070311_exp4_zA", AuxData.Type.PIA_SOMA));
	}

	public static void setNetworkLayers(Network n)
	{
		// Read plane information from aux database
		String filename = (String) n.get(0).getMetadata("FILE");
		if (filename == null) return;
		filename = filename.substring(0, filename.indexOf('.'));
		int piaWm = AuxData.getValue(filename, AuxData.Type.PIA_WM);
		int piaSoma = AuxData.getValue(filename, AuxData.Type.PIA_SOMA);
		int piaLL4 = AuxData.getValue(filename, AuxData.Type.PIA_LOWERL4);
		System.out.println("File " + filename + ", " + piaWm + "," + piaSoma + "," + piaLL4);
		n.setLayers(new Layers(piaSoma, piaSoma-piaLL4, 0, piaSoma-piaWm));
	}

	public static double[] getPlanes(File f) {
		// Read plane information from aux database
		String filename = f.getName();
		if (filename == null) return new double[4];
		filename = filename.substring(0, filename.indexOf('.'));
		int piaWm = AuxData.getValue(filename, AuxData.Type.PIA_WM);
		int piaSoma = AuxData.getValue(filename, AuxData.Type.PIA_SOMA);
		int piaLL4 = AuxData.getValue(filename, AuxData.Type.PIA_LOWERL4);
		System.out.println("File " + filename + ", " + piaWm + "," + piaSoma + "," + piaLL4);
		
		// convert to relative coordinates (soma at y=0)
		double[] planes = new double[] {
				piaSoma, piaSoma-piaLL4, 0, piaSoma-piaWm
		};
		return planes;
	}

}
